package com.arpa.wms.service.impl;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.StrUtil;
import cn.jpush.api.utils.StringUtils;
import com.arpa.ntocc.common.common.util.CommonUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.wms.cache.GoodsCache;
import com.arpa.wms.domain.dto.GoodsPnDTO;
import com.arpa.wms.domain.entity.Goods;
import com.arpa.wms.domain.entity.GoodsPn;
import com.arpa.wms.domain.vo.GoodsPnVo;
import com.arpa.wms.mapper.GoodsPnMapper;
import com.arpa.wms.service.IGoodsPnService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品PN码 服务实现类
 * </p>
 *
 * @author leo
 * @since 2021-08-24
 */
@Service
public class GoodsPnServiceImpl extends ServiceImpl<GoodsPnMapper, GoodsPn> implements IGoodsPnService {

    @Autowired
    private GoodsCache goodsCache;

    @Override
    public GoodsPnVo queryListSum(GoodsPnDTO dto) {
        return baseMapper.queryListSum(dto);
    }

    @Override
    public List<GoodsPnVo> queryList(GoodsPnDTO dto) {    //排序字段名需要驼峰转数据库下划线类型字段名
        if (StringUtils.isNotEmpty(dto.getSortField())) {
            dto.setSortField(CommonUtil.camel2Underline(dto.getSortField()));
        }
        List<GoodsPnVo> vos = baseMapper.queryList(dto);

        return vos;
    }

    /**
     * 根据pn码查询商品编码
     *
     * @param shipmentCode 入驻商编码
     * @param pn           pn
     * @return java.lang.String
     * @author liuxr
     * @date 2021/8/25 10:50
     */
    @Override
    public String getGoodsCode(String shipmentCode, String pn) {
        GoodsPn goodsPn = getOne(new LambdaQueryWrapper<GoodsPn>()
                .select(GoodsPn::getGoodsCode)
                .eq(GoodsPn::getShipmentCode,shipmentCode)
                .eq(GoodsPn::getPn,pn));
        if(goodsPn!=null) {
            return goodsPn.getGoodsCode();
        }
        return "";
    }

    /**
     * 根据pn码查询商品编码
     *
     * @param pn pn
     * @return java.lang.String
     * @author liuxr
     * @date 2021/8/25 10:50
     */
    @Override
    public Set<String> getGoodsCodeByPn(String pn, List<String> shipmentCodes) {
        List<GoodsPn> goodsPns =this.list(new LambdaQueryWrapper<GoodsPn>()
                .eq(GoodsPn::getPn,pn)
                .eq(GoodsPn::getGroupCode,UserUtil.getBranchCode())
                .in(GoodsPn::getShipmentCode,shipmentCodes));
        if(IterUtil.isNotEmpty(goodsPns)){
            return goodsPns.stream().map(GoodsPn::getGoodsCode).collect(Collectors.toSet());
        }
        return null;

    }

    /**
     * 根据pn码和货主查询商品条形码
     * @author rfwang
     * @date 2021/9/1 15:26
     * @param shipmentCode 入驻商编码
     * @param pn pn
     * @return java.lang.String
     */
    @Override
    public String getGoodsBarCode(String shipmentCode,String pn){
        String goodsBarCode = StrUtil.EMPTY;
        String goodsCode = this.getGoodsCode(shipmentCode, pn);
        Goods goods = goodsCache.getObj(goodsCode);
        if (goods != null){
            goodsBarCode = goods.getBarCode();
        }
        return goodsBarCode;
    }

    /**
     * 根据pn码和货主查询商品信息
     * @author rfwang
     * @date 2022/5/30 16:44
     * @param shipmentCode 入驻商编码
     * @param pn pn
     */
    @Override
    public Goods getGoods(String shipmentCode, String pn){
        Goods goods ;
        String goodsCode = this.getGoodsCode(shipmentCode, pn);
        goods = goodsCache.getObj(goodsCode);
        return goods;
    }
}
